package com.atguigu.serviceedu.controller.front;


import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.servicebase.vo.UcenterMember;
import com.atguigu.serviceedu.entity.EduComment;
import com.atguigu.serviceedu.feign.UcenterFeignService;
import com.atguigu.serviceedu.service.EduCommentService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author wyj
 * @since 2021-12-23
 */
@RestController
@RequestMapping("/serviceedu/eduComment")
//@CrossOrigin
public class EduCommentController {
    @Autowired
    private EduCommentService commentService;

    @Autowired
    private UcenterFeignService ucenterFeignService;

    //根据课程id查询评论列表
    @ApiOperation(value = "评论分页列表")
    @GetMapping("getCommentPage/{page}/{limit}")
    public R getCommentPage(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,

            @ApiParam(name = "courseQuery", value = "查询对象", required = false)
                    String courseId) {
        Map<String, Object> map = commentService.getCommentPageBycourseId(page,limit,courseId);

        return R.ok().data(map);
    }

    @ApiOperation(value = "添加评论")
    @PostMapping("auth/save")
    public R save(@RequestBody EduComment comment, HttpServletRequest request) {
        String memberId = JwtUtils.getMemberIdByJwtToken(request);

        if(StringUtils.isEmpty(memberId)) {
            return R.error().code(28004).message("请登录");
        }
        comment.setMemberId(memberId);

        UcenterMember ucenterClientInfo = ucenterFeignService.getInfo(memberId);

        comment.setNickname(ucenterClientInfo.getNickname());
        comment.setAvatar(ucenterClientInfo.getAvatar());

        commentService.save(comment);
        return R.ok();
    }
}

